package edu.sysu.cshare.util.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JToolTip;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import edu.sysu.cshare.control.HomeworkManager;
import edu.sysu.cshare.gui.Dlg_HomeworkDetail;
import edu.sysu.cshare.gui.Pnl_Homework;
import edu.sysu.cshare.model.Homework;
import edu.sysu.cshare.util.Mis;
import edu.sysu.cshare.util.gui.GUI.MultiLineToolTip;

public class Table_Homework extends JTable {

	private HomeworkManager homeworkManager = null;

	public Table_Homework(TableModel dm, HomeworkManager homeworkManager) {
		super(dm);
		this.homeworkManager = homeworkManager;

		// 排序
		this.setAutoCreateRowSorter(true);

		// 隐藏第一列(id)
		this.getColumnModel().getColumn(0).setMaxWidth(0);
		this.getColumnModel().getColumn(0).setMinWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);

		GUI.FitTableColumns(this);

		this.setRowHeight(20);

		this.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				int row = rowAtPoint(e.getPoint());
				int col = columnAtPoint(e.getPoint());

				if (row < 0)
					return;

				Homework homework = HomeworkManager
						.getHomeworkById((Long) getValueAt(row, 0));

				try {
					if (Pnl_Homework.Naming.getClickSubmit().equals(
							getColumnName(col))) {
						if (homework.shouldUpload()) {
							String path = GUI.getFilePath();
							Table_Homework.this.homeworkManager.submit(
									homework, path);
						} else {
							Table_Homework.this.homeworkManager.submit(
									homework, null);
						}

					} else if (Pnl_Homework.Naming.getReDownload().equals(
							getColumnName(col))) {
						if (!homework.shouldUpload())
							return;
						String localDir = GUI.getDirPath();
						Table_Homework.this.homeworkManager.downloadMine(
								homework, localDir);
					} else if (Pnl_Homework.Naming.getReSubmit().equals(
							getColumnName(col))) {
						if (!homework.shouldUpload())
							return;
						String localDir = GUI.getFilePath();
						Table_Homework.this.homeworkManager.submit(homework,
								localDir);
					} else if (Pnl_Homework.Naming.getViewDetail().equals(
							getColumnName(col))) {
						GUI.showModalDialog(new Dlg_HomeworkDetail(homework,
								Table_Homework.this.homeworkManager));
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
				Table_Homework.this.updateUI();
			}
		});

	}

	@Override
	public String getToolTipText(MouseEvent event) {
		String tip = null;
		java.awt.Point p = event.getPoint();
		int rowIndex = rowAtPoint(p);
		int colIndex = columnAtPoint(p);
		int realColumnIndex = convertColumnIndexToModel(colIndex);
		// System.out.println(rowIndex+" "+this.getRowCount());
		if (rowIndex < this.getRowCount() && rowIndex >= 0) {
			Homework homework = HomeworkManager.getHomeworkById((Long) (this
					.getValueAt(rowIndex, 0)));
			String s1 = homework.getCourse().getName();
			String s2 = homework.getCourse().getTeacher().getName();
			String s3 = homework.getDescrip();
			String s4 = Mis.formatCalendar(homework.getStartDate());
			int s5 = homework.getMaxSize();
			String s6 = homework.getExtRestrict();
			String s7 = homework.getSubmitByGroup() ? "是" : "否";
			String s8 = homework.getSubmitMethod();
			String s9 = homework.getRemark();
			tip = String.format("课程名称:%s\n" + "任课老师:%s\n" + "作业描述:%s\n"
					+ "截止日期:%s\n" + "大小限制:%dKB\n" + "格式限制:%s\n" + "是否按组提交:%s\n"
					+ "提交方法:%s\n" + "备注:%s\n", s1, s2, s3, s4, s5, s6, s7, s8,
					s9);
			if (getColumnName(realColumnIndex).equals(
					Pnl_Homework.Naming.getClickSubmit())) {
				tip += Pnl_Homework.Naming.getSubTooptip();
			}
		}
		return tip;
	}

	@Override
	public JToolTip createToolTip() {
		MultiLineToolTip tip = new MultiLineToolTip();
		return tip;
	}

	@Override
	protected JTableHeader createDefaultTableHeader() {

		return new JTableHeader(columnModel) {
			public String getToolTipText(MouseEvent e) {
				String tip = null;
				java.awt.Point p = e.getPoint();
				int index = columnModel.getColumnIndexAtX(p.x);
				int realIndex = columnModel.getColumn(index).getModelIndex();
				if (getColumnName(realIndex).equals(
						Pnl_Homework.Naming.getClickSubmit())) {
					tip = Pnl_Homework.Naming.getSubTooptip();
				}
				return tip;
			}
		};
	}

}
